package com.library.uiframework.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.library.uiframework.R;

/**
 * 将其他不带有标题栏的fragment包装成带标题栏的fragment
 *
 * @param <FRAGMENT> 带包装的Fragment泛型
 * @author hongkai.qian
 * @version 1.0.0
 * @since 15/7/27
 */
public abstract class WrapBaseFragment<FRAGMENT extends BaseFragment> extends ActionBarLayoutFragment {

    private static final String TAG = "WrapBaseFragment";

    protected static final String KEY_FRAGMENT_TITLE = "key_fragment_title";
    protected static final String KEY_FRAGMENT_TITLE_RES_ID = "key_fragment_title_res_id";
    private static final String KEY_CLASS_NAME = "key_class_name";

    /**
     * 内嵌的fragment
     */
    protected FRAGMENT mFragment;


    /**
     * 生成基本参数
     *
     * @param fragmentTitle title
     * @param fragmentClass class
     * @return bundle
     */
    public static Bundle makeBaseBundle(String fragmentTitle, Class<? extends BaseFragment> fragmentClass) {
        Bundle bundle = new Bundle();
        if (fragmentTitle != null) {
            bundle.putString(KEY_FRAGMENT_TITLE, fragmentTitle);
        }
        bundle.putString(KEY_CLASS_NAME, fragmentClass.getName());
        return bundle;
    }

    /**
     * 生成基本参数
     *
     * @param fragmentTitleResId fragmentTitleResId
     * @param fragmentClass      fragmentClass
     * @return bundle
     */
    public static Bundle makeBaseBundle(int fragmentTitleResId, Class<? extends BaseFragment> fragmentClass) {
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_FRAGMENT_TITLE_RES_ID, fragmentTitleResId);
        bundle.putString(KEY_CLASS_NAME, fragmentClass.getName());
        return bundle;
    }

    /**
     * 自定义包装layout，必须有id为layout_fragment_container的容器
     *
     * @return layout resource id
     */
    protected int wrapBaseLayoutId() {
        return R.layout.common_fragment_empty;
    }

    /**
     * @return 包装的是哪个fragment 类型
     */
    protected abstract Class wrapFragmentClass();

    @Override
    protected View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            if (arguments.containsKey(KEY_FRAGMENT_TITLE)) {
                setTitle(arguments.getString(KEY_FRAGMENT_TITLE));
            } else {
                int titleResId = arguments.getInt(KEY_FRAGMENT_TITLE_RES_ID);
                if (titleResId != 0) {
                    setTitle(titleResId);
                } else {
                    setTitle(null);
                }
            }
        }
        return inflater.inflate(wrapBaseLayoutId(), container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final Bundle arguments = getArguments();
        Class clazz = null;
        if (arguments != null && arguments.containsKey(KEY_CLASS_NAME)) {
            try {
                clazz = Class.forName(arguments.getString(KEY_CLASS_NAME));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        if (clazz == null) {
            clazz = wrapFragmentClass();
        }
        if (clazz != null) {
            try {
                Bundle bundle = resetBundle();
                if(arguments != null){
                    bundle.putAll(arguments);
                }
                mFragment = (FRAGMENT) clazz.newInstance();
                mFragment.setArguments(bundle);
                getChildFragmentManager().beginTransaction().replace(R.id.layout_fragment_container, mFragment).commitAllowingStateLoss();
            } catch (Throwable e) {
                Log.e(TAG, "newInstance " + mFragment + " e:" + e.toString());
            }
        }
    }

    protected Bundle resetBundle(){
        return new Bundle();
    }

}
